﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using JOIN.Rights.BLL;
using JOIN.Rights.Model;
using JOIN.Rights.BLL.Cache;
using System.Data;
using System.Web.Script.Serialization;

namespace JOIN.Rights.Web.Services
{
    /// <summary>
    /// Summary description for CodeNameService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class CodeNameService : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        /// <summary>
        /// 获取企业列表
        /// </summary>
        /// <param name="Code"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        [WebMethod]
        public string GetCompany(string Code, int pageSize, int pageIndex)
        {
            List<CodeNameItem> list = new List<CodeNameItem>();
            if (!CodeNameCache.Instance.TryGetValue("company", out list))
            {
                CompanyBLL bll = new CompanyBLL(new CompanyInfo());
                DataTable dt = bll.SelectAllCompany();
                list = new List<CodeNameItem>();
                foreach (DataRow dr in dt.Rows)
                {
                    list.Add(new CodeNameItem(dr["CompanyID"].ToString(), dr["CompanyName"].ToString()));
                }
                CodeNameCache.Instance.Add("company", list);
            }

            return ProcessCodeNameList(Code, pageSize, pageIndex, list);  
        }

        [WebMethod]
        public string GetOrg(string Code, int pageSize, int pageIndex)
        {
            List<CodeNameItem> list = new List<CodeNameItem>();
            if (!CodeNameCache.Instance.TryGetValue("Organization", out list))
            {
                OrgBLL bll = new OrgBLL(new OrgInfo());
                DataTable dt = bll.SelectAllOrg();
                list = new List<CodeNameItem>();
                foreach (DataRow dr in dt.Rows)
                {
                    list.Add(new CodeNameItem(dr["OrgID"].ToString(), dr["OrgFullName"].ToString()));
                }
                CodeNameCache.Instance.Add("Organization", list);
            }

            return ProcessCodeNameList(Code, pageSize, pageIndex, list);
        }
        /// <summary>
        /// 获取项目列表
        /// </summary>
        /// <param name="Code">查询关键字</param>
        /// <param name="pageSize">分页大小</param>
        /// <param name="pageIndex">页次</param>
        /// <returns></returns>
        [WebMethod]
        public string GetProjects(string Code, int pageSize, int pageIndex)
        {
            List<CodeNameItem> list=new ProjectBLL(new ProjectInfo()).LoadCodeNameFromCache();

           return  ProcessCodeNameList(Code, pageSize, pageIndex, list);            
        }
        /// <summary>
        /// 获取角色信息
        /// </summary>
        /// <param name="Code"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        [WebMethod]
        public string GetRoles(string Code, int pageSize, int pageIndex)
        {
            List<CodeNameItem> list = new List<CodeNameItem>();
            if (!CodeNameCache.Instance.TryGetValue("roles", out list))
            {
                RoleBLL bll = new RoleBLL(new RoleInfo());
                DataTable dt = bll.SelectAllRoles();
                list = new List<CodeNameItem>();
                foreach (DataRow dr in dt.Rows)
                {
                    list.Add(new CodeNameItem(dr["RoleID"].ToString(), dr["RoleName"].ToString()));
                }
                CodeNameCache.Instance.Add("roles", list);
            }

            return ProcessCodeNameList(Code, pageSize, pageIndex, list);
        }

        #region [内部处理方法 席吉波 2010-05-25]

        private string ProcessCodeNameList(string Code, int count, int indexNumber, IList<CodeNameItem> items)
        {
            if (count == 0)
            {
                count = 10;
            }

            int pageSize = count;
            var item_temp = new List<CodeNameItem>();
            if (count > 1)
            {
                item_temp = (from a in items
                             where a.Code.Contains(Code) || a.Name.Contains(Code)
                             orderby a.Code
                             select a).ToList<CodeNameItem>();
            }
            else
            {
                if (count == 1)
                    item_temp = (from a in items
                                 where a.Code == Code
                                 select a).ToList<CodeNameItem>();
            }

            Decimal pageCount = Math.Ceiling((Convert.ToDecimal(item_temp.Count) / count));

            IList<CodeNameItem> list = new List<CodeNameItem>(count + 1);

            int pageIndex = indexNumber;
            int itemCount = items.Count;
            int startIndex = 0;
            int endIndex = 0;

            setIndex(item_temp.Count, pageSize, pageIndex, out startIndex, out endIndex);

            for (int i = startIndex; i < endIndex; i++)
            {
                list.Add(new CodeNameItem(item_temp[i].Code, item_temp[i].Name));
            }

            if (list.Count > 0)
                list.Add(new CodeNameItem("PAGE_COUNT", pageCount.ToString()));

            return new JavaScriptSerializer().Serialize(list);

        }

        private void setIndex(int recordCount, int pageSize, int pageIndex, out int startIndex, out int endIndex)
        {
            startIndex = 0;
            endIndex = 0;

            if (recordCount <= pageSize)
            {
                endIndex = recordCount;
                return;
            }
            if (recordCount > pageSize && recordCount <= pageSize * pageIndex)
            {
                startIndex = pageSize * (pageIndex - 1);
                endIndex = recordCount;
            }
            if (recordCount > pageSize && recordCount > pageSize * pageIndex)
            {
                startIndex = pageSize * (pageIndex - 1);
                endIndex = pageSize * pageIndex;
            }
        }

        #endregion
    }
}
